<?php

namespace App\Admin\Controllers;

use App\Admin\Models\TimelineConfigModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Models\ContentModel;

class MemorabiliaController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '大事记';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'index'  => '这里展示的是大事记',
        'show'   => '大事记详情',
        'edit'   => '请在此处编辑大事记',
        'create' => '请创建新的大事记',
    ];

    /**
     * @var int 内容类型，1-中心简介，2-大事记，3-科研方向，4-新闻，5-公告，6-联系我们，7-招聘联系方式，8-友情链接，
     * 9-首页轮播图，10-学术讲座，11-学术会议，12-学术快讯，13-近期论文，14-链接栏目，15-合作伙伴
     */
    private $contentType = 2;

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ContentModel);

        $timelineList = TimelineConfigModel::getConfigList();
        !empty($timelineList) && $timelineList = array_column($timelineList, 'title_zh', 'timeline_id');

        // 添加筛选条件
        $grid->model()->where('content_type', '=', $this->contentType);
        $grid->model()->orderBy('order', 'asc');
        $grid->model()->orderBy('content_id', 'desc');

//        $grid->column('content_id', __('ID'))->width(60)->sortable();
        $grid->column('order', __('优先级'))->width(110)->sortable();
        $grid->column('title_zh', __('标题-中文'));
        $grid->column('title_en', __('标题-英文'));
        $grid->column('timeline_id', __('时间线'))->width(150)->display(function ($timelineId) use ($timelineList) {
            return !empty($timelineList[$timelineId]) ? $timelineList[$timelineId] : '';
        });
        $grid->column('created_at', __('创建时间'))->sortable()->hide();
        $grid->column('updated_at', __('更新时间'))->sortable()->hide();

        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('title_zh', __('标题-中文'));
            $filter->like('title_en', __('标题-英文'));

        });

        // 禁用导出数据按钮
        $grid->disableExport();

        // 设置样式
        \Encore\Admin\Admin::style('.column-__row_selector__ {width: 70px;} .column-__actions__ {width: 70px;}');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ContentModel::findOrFail($id));

//        $show->field('content_id', __('ID'))->setWidth(2, 2);
        $show->field('order', __('优先级'))->setWidth(2, 2);
        $show->field('title_zh', __('标题-中文'))->setWidth(8, 2);
        $show->field('title_en', __('标题-英文'))->setWidth(8, 2);
        $show->field('content_zh', __('详情-中文'))->unescape()->as(function ($contentZh) {
            return $contentZh;
        })->setWidth(8, 2);
        $show->field('content_en', __('详情-英文'))->unescape()->as(function ($contentEn) {
            return $contentEn;
        })->setWidth(8, 2);
        $show->field('timeline_id', __('时间线'))->unescape()->as(function ($timelineId) {
            $timeline = TimelineConfigModel::getConfigById($timelineId);

            return !empty($timeline) ? $timeline['title_zh'] : '';
        })->setWidth(2, 2);
        $show->field('created_at', __('创建时间'))->setWidth(2, 2);
        $show->field('updated_at', __('更新时间'))->setWidth(2, 2);

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ContentModel(['content_type' => $this->contentType]));

        $params = request()->route()->parameters();
        $contentId = !empty($params['memorabilium']) ? (int)$params['memorabilium'] : 0;

        $form->number('order', __('优先级'))->min(1)->setWidth(2, 2)->default(1)->rules('required|min:1')->required();

        // 时间线选项
        $options = TimelineConfigModel::getConfigList();
        !empty($options) && $options = array_column($options, 'title_zh', 'timeline_id');
        $form->select('timeline_id', __('时间线'))->options($options)->setWidth(2, 2)->required();

        $form->text('title_zh', __('标题-中文'))->rules('required|min:1|max:120')->maxlength(120)
            ->placeholder('输入标题-中文，字符长度限制在120字符内')->setWidth(8, 2)->required();
        $form->text('title_en', __('标题-英文'))->rules('nullable|min:1|max:120')->maxlength(120)
            ->placeholder('输入标题-英文，字符长度限制在120字符内')->setWidth(8, 2);
        $form->froala('content_zh', __('详情-中文'))->setContentId($contentId)->setContentType(2)->rules('required|min:1')->setWidth(8, 2)->required();
        $form->froala('content_en', __('详情-英文'))->setContentId($contentId)->setContentType(2)->setWidth(8, 2);

        return $form;
    }
}
